/**
 * activity for the map view
 * 
 * @author patrick
 * @version 1.0
 * 
 */
package com.BlackMesa.controllers;

import java.util.ArrayList;
import java.util.List;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;

import com.BlackMesa.models.OurLocation;
import com.BlackMesa.models.Task;
import com.BlackMesa.models.User;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class MapActive extends MapActivity {
	private MapView mapView;
	private MapController mc;
    private ArrayList<OurLocation> p;
    
    class MapOverlay extends com.google.android.maps.Overlay
    {
    	
//        @Override
//        public boolean draw(Canvas canvas, MapView mapView, 
//        boolean shadow, long when) 
//        {
//            super.draw(canvas, mapView, shadow);                   
// 
//            //---translate the GeoPoint to screen pixels---
//        	Point screenPts = new Point();
//        	mapView.getProjection().toPixels(p, screenPts);
//        	
// 
//            //---add the marker---
//
//        	Bitmap bmp = BitmapFactory.decodeResource(
//                    getResources(), R.drawable.androidmarker);  
//            canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);
//            return true;
//        }
        
        
    } 
    public void addMyPin(GeoPoint point)
    {    
            Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);
            OurItemOverlay itemizedoverlay = new OurItemOverlay(drawable,this.getBaseContext());

            OverlayItem overlayitem = new OverlayItem(point,null,null);
            itemizedoverlay.addOverlay(overlayitem);

            mapView.getOverlays().add(itemizedoverlay);
           mapView.postInvalidate();
    }
	
	/**
	 * method for route being displayed
	 * 
	 * @return
	 */
	protected boolean isRouteDisplayed() {
	    return false;
	}
	
	/**
	 * sets the view to map view and enables zoom
	 * 
	 * @param savedInstanceState
	 */
	public void onCreate(Bundle savedInstanceState) {  	
        super.onCreate(savedInstanceState);
	    setContentView(R.layout.map_view);
	    mapView = (MapView) findViewById(R.id.googlemapview);
        mc = mapView.getController();
        mapView.setBuiltInZoomControls(true);
        /*
        String coordinates[] = {"33.775506", "-84.396719"};
        double lat = Double.parseDouble(coordinates[0]);
        double lng = Double.parseDouble(coordinates[1]);
 
        p = new GeoPoint(
            (int) (lat * 1E6), 
            (int) (lng * 1E6));
	    mc.animateTo(p);
        */
        
	    
        mc.setZoom(15); 
      //---Add a location marker---
        MapOverlay mapOverlay = new MapOverlay();
        p = locationSender.getLoc();
        if(!p.isEmpty()){
		    for(OurLocation z:p){
		    	mc.animateTo(z.getLoc());
		    	addMyPin(z.getLoc());
		    	Log.d(""+z.getLoc().getLatitudeE6(), ""+z.getLoc().getLongitudeE6());
		    }
        }
        mc.setZoom(15); 
      //---Add a location marker---
        List<Overlay> listOfOverlays = mapView.getOverlays();
       // listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);        
 
        mapView.postInvalidate();
	}
}
